package com.hirelinfo.form.student;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hirelinfo.dto.student.Student;
import com.hirelinfo.service.student.StudentManagerService;

@Controller
public class Register {

	@Autowired
	StudentManagerService studentManagerService;

	@RequestMapping("/studentRegisterpage")
	public ModelAndView studentForm() {
		ModelAndView mav = new ModelAndView("studentRegister");
		mav.addObject("registerForm", new Student());

		return mav;

	}

	@RequestMapping(value = "/StudentRegister")
	public ModelAndView registerForm(
			@Valid @ModelAttribute(value = "registerForm") Student student,
			BindingResult result) {

		ModelAndView mav = new ModelAndView();
		if (result.hasErrors()) {
			mav.setViewName("studentRegister");
			mav.addObject("registerForm", student);
			System.out.println("Errror has been  occurred");
			return mav;

		}

		studentManagerService.saveNewStudent(student);

		mav.setViewName("studentSuccessPage");
		mav.addObject("student", student);

		mav.addObject("status", "Student Form successfully");

		return mav;
	}
}
